package com.dearbinge.opentapi.content.commodity;


import com.baomidou.mybatisplus.plugins.Page;
import com.dearbinge.data.api.commodity.CommodityCommentService;
import com.dearbinge.data.pojo.commodity.vo.CommodityCommentResponseVO;
import com.dearbinge.data.pojo.commodity.vo.CommodityCommentUpdate;
import com.dearbinge.data.pojo.commodity.vo.CommodityCommentVO;
import com.dearbinge.data.pojo.commodity.vo.CommodityDetailCommentVO;
import com.dearbinge.utils.BeanValidator;
import com.dearbinge.utils.ResultMsg;
import com.dearbinge.utils.enums.ResultStatusCodeEnum;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.HashMap;
import java.util.Map;

/**
 * 商品评价控制层
 *
 * @author wes
 * @date 18:22 2019/1/4
 */
@Api(tags = "2.5、商品-评价")
@RestController
@CrossOrigin
@RequestMapping("/commodityComment")
@EnableAutoConfiguration
public class CommodityCommentController extends WebMvcConfigurerAdapter {


    @Autowired
    private CommodityCommentService commodityCommentService;

    @ApiOperation(value = "买家评价", notes = "买家评价")
    @ApiResponses({
            @ApiResponse(code = 5001, message = "参数为空"),
            @ApiResponse(code = 5002, message = "参数错误")
    })
    @RequestMapping(value = "addCommodityComment", method = RequestMethod.POST)
    public ResultMsg<String> addCommodityComment(@RequestBody CommodityCommentVO commodityCommentVO) {
        try {
            return commodityCommentService.insert(commodityCommentVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), "系统异常");
    }

    @ApiOperation(value = "修改", notes = "修改")
    @ApiResponses({
            @ApiResponse(code = 5001, message = "参数为空"),
            @ApiResponse(code = 5002, message = "参数错误")
    })

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public ResultMsg<String> update( @RequestBody CommodityCommentUpdate commodityCommentUpdate) {
        Integer commodityCommentId = commodityCommentUpdate.getCommodityCommentId();
        String content = commodityCommentUpdate.getContent();
        String[] imgArray = commodityCommentUpdate.getImgArray();
        Integer type = commodityCommentUpdate.getType();
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("content", content);
            paramMap.put("commodityCommentId", commodityCommentId);
            paramMap.put("type", type);
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }
            return commodityCommentService.update(content, imgArray, type, commodityCommentId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), "系统异常");
    }

    /**
     * 根据ID查询
     */
    @ApiOperation(value = "根据ID查询对象", notes = "根据ID查询")
    @ApiImplicitParam(name = "commodityCommentId", value = "商品评论ID", paramType = "query", required = true)
    @ApiResponse(response = CommodityCommentResponseVO.class, code = 200, message = "返回结果参数")
    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public ResultMsg<CommodityCommentResponseVO> getById(Integer commodityCommentId) {
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("commodityCommentId", commodityCommentId);
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }
            CommodityCommentResponseVO commodityCommentResponseVO = commodityCommentService.getById(commodityCommentId);
            if (commodityCommentResponseVO == null) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5002.getCode(), "商品评论id不存在");
            }
            return ResultMsg.ok(commodityCommentResponseVO);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), "系统异常");
    }


    @ApiOperation(value = "分页查商品评论", notes = "分页查商品评论")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "commodityId", value = "商品ID", paramType = "query", required = true),
            @ApiImplicitParam(name = "commentType", value = "0：全部(默认)；1：好评；2：中评；3：差评；4：图片；5：追加评价",
                    paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录数", paramType = "query")
    })
    @ApiResponses(value = {
            @ApiResponse(response = CommodityCommentResponseVO.class, code = 200, message = "返回结果参数")
    })
    @RequestMapping(value = "/listCommodityComment", method = RequestMethod.GET)
    public ResultMsg<CommodityDetailCommentVO> listCommodityComment(Integer commodityId, Integer commentType, Integer current, Integer size) {
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("commodityId", commodityId);
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }
            CommodityDetailCommentVO commodityDetailCommentVO = commodityCommentService.listCommodityComment(commodityId, commentType, current, size);
            return ResultMsg.okData(commodityDetailCommentVO, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), "系统异常");
    }


    @ApiOperation(value = "我的评价（用户）", notes = "我的评价（用户）")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", paramType = "query", required = true),
            @ApiImplicitParam(name = "commentType", value = "0：全部(默认)；1：好评；2：中评；3：差评；4：图片；5：追加评价",
                    paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页", paramType = "query"),
            @ApiImplicitParam(name = "size", value = "页面记录数", paramType = "query")
    })
    @ApiResponses(value = {
            @ApiResponse(response = CommodityCommentResponseVO.class, code = 200, message = "返回结果参数"),
            @ApiResponse(code = 5001, message = "参数为空"),
    })
    @RequestMapping(value = "/listMyComment", method = RequestMethod.GET)
    public ResultMsg<Page> listMyComment(Integer userId, Integer commentType, Integer current, Integer size) {
        try {
            //校验参数非空
            Map<String, Object> paramMap = new HashMap<String, Object>(16);
            paramMap.put("userId", userId);
            Map<String, Object> checkParam = BeanValidator.checkParam(paramMap);
            if (!(boolean) checkParam.get(BeanValidator.FLAG)) {
                return ResultMsg.error(ResultStatusCodeEnum.CODE5001.getCode(), checkParam.get("name") + "参数为空");
            }
            Page result = commodityCommentService.listMyComment(userId, commentType, current, size);
            return ResultMsg.okData(result, ResultStatusCodeEnum.SUCCESS.getDesc());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultMsg.error(ResultStatusCodeEnum.CODE500.getCode(), "系统异常");
    }


}
